package com.czwj.action;

import java.net.Socket;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.czwj.annotation.SystemControllerLog;
import com.czwj.entities.DataGridData;
import com.czwj.entities.Equipment;
import com.czwj.entities.EquipmentMark;
import com.czwj.entities.OperateResult;
import com.czwj.entities.PageBean;
import com.czwj.service.EquipmentMarkService;
import com.czwj.service.EquipmentService;
import com.czwj.socket.OilPressureSocket;
import com.czwj.util.TextUtil;

/**
* @ClassName: EquipmentAction
* @Description: TODO
* @author H
* @date 2020年4月9日
*
*/
@Controller
public class EquipmentAction{
	@Resource
	EquipmentService equipmentService;
	@Resource
	EquipmentMarkService equipmentMarkService;
	
	@RequestMapping(value = "/equipmentList",method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<Equipment> equipment(@RequestParam(value = "page",required = false) String page,
			@RequestParam(value = "rows",required = false) String rows,
			@RequestParam(value = "projectitemid",required = false) Integer projectitemid,
			@RequestParam(value = "name",required = false) String name,
			@RequestParam(value = "starttime",required = false) String starttime,
			@RequestParam(value = "dailytime",required = false) String dailytime){
		DataGridData<Equipment> result = new DataGridData();
		Map<String,Object> map = new HashMap<String,Object>();
		PageBean pageBean = null;
		if(TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows)){
			pageBean = new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		}
		if(pageBean != null){
			map.put("start",pageBean.getStart());
			map.put("size",pageBean.getPageSize());
		}
		map.put("projectitemid",projectitemid);
		map.put("name",name);
		map.put("starttime",starttime);
		map.put("dailytime",dailytime);
		List<Equipment> list = equipmentService.search(map);
		result.setRows(list);
		result.setTotal(equipmentService.count(map));
		return result;
	}
	
	@RequestMapping(value = "/equipments.do",method = RequestMethod.POST)
	@ResponseBody
	public List<Equipment> equipments(@RequestParam(value = "projectitemid",required = false) Integer projectitemid){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("projectitemid",projectitemid);
		List<Equipment> list = equipmentService.search(map);
		return list;
	}
	
	@SystemControllerLog(description = "修改设备")
	@RequestMapping(value = "/equipmentSave.do",method = RequestMethod.POST)
	@ResponseBody
	public OperateResult equipmentSave(@RequestBody Equipment equipment){
		OperateResult result = new OperateResult();
		int count = 0;
		if(equipment.getId() != null && equipment.getId() > 0){
			count = equipmentService.update(equipment);
		}else{
			count = equipmentService.insert(equipment);
		}
		result.setSuccess(count > 0);
		return result;
	}
	
	@SystemControllerLog(description = "删除设备")
	@RequestMapping(value = "equipmentDelete.do",method = RequestMethod.POST)
	@ResponseBody
	public OperateResult equipmentDelete(@RequestParam("id") int id){
		OperateResult result = new OperateResult();
		int count = equipmentService.delete(id);
		result.setSuccess(count > 0);
		return result;
	}
	
	@RequestMapping(value = "/equipmentMarkList",method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<EquipmentMark> equipmentMarkList(@RequestParam(value = "page",required = false) String page,
			@RequestParam(value = "rows",required = false) String rows,
			@RequestParam(value = "projectitemid",required = false) Integer projectitemid){
		DataGridData<EquipmentMark> result = new DataGridData();
		Map<String,Object> map = new HashMap<String,Object>();
		PageBean pageBean = null;
		if(TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows)){
			pageBean = new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		}
		if(pageBean != null){
			map.put("start",pageBean.getStart());
			map.put("size",pageBean.getPageSize());
		}
		map.put("projectitemid",projectitemid);
		List<EquipmentMark> list = equipmentMarkService.search(map);
		result.setRows(list);
		result.setTotal(equipmentMarkService.count(map));
		return result;
	}
	
	@SystemControllerLog(description = "修改设备维保")
	@RequestMapping(value = "/equipmentMarkSave.do",method = RequestMethod.POST)
	@ResponseBody
	public OperateResult equipmentMarkSave(@RequestParam(value = "images",required = false) MultipartFile[] images,
			@RequestParam(value = "id",required = false) Integer id,
			@RequestParam(value = "equipmentid",required = false) Integer equipmentid,
			@RequestParam(value = "starttime",required = false) String starttime,
			@RequestParam(value = "intervaltime",required = false) String intervaltime,
			@RequestParam(value = "isdaily",required = false) Integer isdaily,
			@RequestParam(value = "remark",required = false) String remark,
			@RequestParam(value = "createtime",required = false) String createtime,HttpServletRequest request){
		OperateResult result = new OperateResult();
		int count = 0;
		count = equipmentMarkService.insertorupdate(id,equipmentid,starttime,intervaltime,isdaily,remark,createtime,images,request);
		/*if(id != null && id > 0){
			EquipmentMark equipmentMark = equipmentMarkService.selectByPrimaryKey(id);
			if(equipmentid != null && equipmentid > 0){
				equipmentMark.setEquipmentid(equipmentid);
			}
			try{
				if(TextUtil.isNotEmpty(starttime)){
					equipmentMark.setStarttime(sdf2.parse(starttime));
				}
				if(TextUtil.isNotEmpty(createtime)){
					equipmentMark.setCreatetime(sdf2.parse(createtime));
				}
			}
			catch(ParseException e){
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if(TextUtil.isNotEmpty(intervaltime)){
				equipmentMark.setIntervaltime(intervaltime);
			}
			if(isdaily != null && isdaily > 0){
				equipmentMark.setIsdaily(isdaily);
			}
			if(TextUtil.isNotEmpty(remark)){
				equipmentMark.setRemark(remark);
			}
			count = equipmentMarkService.update(equipmentMark);
		}else{
			EquipmentMark equipmentMark = new EquipmentMark();
			equipmentMark.setEquipmentid(equipmentid);
			try{
				equipmentMark.setStarttime(sdf2.parse(starttime));
				equipmentMark.setCreatetime(sdf2.parse(createtime));
			}
			catch(ParseException e){
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			equipmentMark.setIntervaltime(intervaltime);
			equipmentMark.setIsdaily(isdaily);
			equipmentMark.setRemark(remark);
			equipmentMark.setStatus(1);
			count = equipmentMarkService.insert(equipmentMark);
		}*/
		result.setSuccess(count > 0);
		return result;
	}
	
	@SystemControllerLog(description = "删除设备维保")
	@RequestMapping(value = "equipmentMarkDelete.do",method = RequestMethod.POST)
	@ResponseBody
	public OperateResult equipmentMarkDelete(@RequestParam("id") int id){
		OperateResult result = new OperateResult();
		int count = equipmentMarkService.delete(id);
		result.setSuccess(count > 0);
		return result;
	}
	@SystemControllerLog(description = "处理设备维保")
	@RequestMapping(value = "equipmentMarkUpdate.do",method = RequestMethod.POST)
	@ResponseBody
	public OperateResult equipmentMarkUpdate(@RequestBody EquipmentMark mark){
		OperateResult result = new OperateResult();
		int count = equipmentMarkService.updateStatus(mark);
		result.setSuccess(count > 0);
		return result;
	}
	
//	@SystemControllerLog(description = "设置上传间隔")
	@RequestMapping(value = "equipmentSetBlank.do",method = RequestMethod.POST)
	@ResponseBody
	public OperateResult equipmentSetBlank(@RequestParam("id") int id,@RequestParam("blank") int blank){
		OperateResult result = new OperateResult();
		Equipment equipment = equipmentService.selectByPrimaryKey(id);
		Socket socket = OilPressureSocket.socketMap.get(equipment.getCode());
		if(socket!=null) {
			OilPressureSocket.sendTimeBlank(socket, blank);
			equipment.setTimeblank(blank);
			int count = equipmentService.update(equipment);
			result.setSuccess(count > 0);
		}else {
			result.setErrMsg("设备不在线");
		}
		return result;
	}
	@RequestMapping(value = "requirePressureData",method = RequestMethod.GET)
	@ResponseBody
	public OperateResult equipmentSetBlank(@RequestParam("id") int id){
		OperateResult result = new OperateResult();
		Equipment equipment = equipmentService.selectByPrimaryKey(id);
		Socket socket = OilPressureSocket.socketMap.get(equipment.getCode());
		result.setSuccess(false);
		if(socket!=null) {
			result.setSuccess(OilPressureSocket.requirePressureData(socket));
		}else {
			result.setErrMsg("设备不在线");
		}
		return result;
	}
}
